/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.resources;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

import pcgen.android.Logger;

public class GameResourceNote  extends GameResourceEx
{
	public GameResourceNote()
	{	
	}
	
	public GameResourceNote(Parcel in)
	{
		super(in);
	}
	
	public GameResourceNote(int type)
	{
		super();
		
		_type = type;
	}
	
	public static final Parcelable.Creator<GameResourceNote> CREATOR = new Parcelable.Creator<GameResourceNote>() 
	{
		public GameResourceNote createFromParcel(Parcel in) 
		{
		    return new GameResourceNote(in);
		}
		
		public GameResourceNote[] newArray(int size) 
		{
		    return new GameResourceNote[size];
		}
	};
	
	public int getType()
	{
		return _type;
	}
	
	public String getText()
	{
		return _text;
	}
	
	public boolean isType(int type)
	{
		return (_type == type);
	}
	
	public JSONObject load(JSONObject data)
		throws JSONException
	{
		setText(getJSONString(data, "text"));
		setType(getJSONInt(data, "type"));
	    
	    return data;
	}
	
	public JSONObject save() 
		throws JSONException
	{
	    JSONObject data = new JSONObject();
	    
	    data.put("text", _text);
	    data.put("type", _type);
	    
	    return data;
	}
	
	public void setText(String text)
	{
		_text = text;
	}
	
	public void setType(int type)
	{
		_type = type;
	}

	@Override
	protected void readFromParcelTransform(Parcel in)
		throws Throwable 
	{
		try
    	{
			setText(in.readString());
			setType(in.readInt());
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "readFromParcelTransform", tr);
    		throw tr;
    	}
	}

	@Override
	protected void writeToParcelTransform(Parcel dest, int flags)
		throws Throwable
	{
		try
    	{
			dest.writeString(_text);
			dest.writeInt(_type);
		}
    	catch (Throwable tr)
    	{
    		Logger.e(TAG, "writeToParcelTransform", tr);
    		throw tr;
    	}
	}
	
	private String _text;
	private int _type;

	private static final String TAG = GameResourceNote.class.getSimpleName();
}
